/**
 * Created by novem on 15/9/25.
 */
function pi(){
    return  3.1415926535898;
}
pow=Math.pow;
sin=Math.sin;
cos=Math.cos;
asin=Math.asin;
min=Math.min;
sqrt=Math.sqrt;
window.onload=function(){
    $(".ui-icon-search").on('click',function(){
        var wname = $("#search").val();
        var url = getUrl();
        var loc = getLoc();
        $.jsonp({
            url: 'http://'+url+'/api/rcar/rcar_list',
            data: {rcarName:wname},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                $.each(data.array,function(index,item) {
                    earthRadius = 6371;
                    var lat0 = (loc.lat * pi()) / 180;
                    var lng0 = (loc.lng * pi()) / 180;
                    var lat1 = (item.rcardimensions * pi()) / 180;
                    var lng1 = (item.rcarLongitude * pi()) / 180;
                    calcLongitude = lng1 - lng0;
                    calcLatitude = lat1 - lat0;
                    stepOne = pow(sin(calcLatitude / 2), 2) + cos(lat0) * cos(lat1) * pow(sin(calcLongitude / 2), 2);
                    stepTwo = 2 * asin(min(1, sqrt(stepOne)));
                    calculatedDistance = earthRadius * stepTwo;
                    item.distance=calculatedDistance.toFixed(1);
                    item.wname = wname;
                });
                var complied_tpl = juicer($("#car_tpl").text());
                var html = complied_tpl.render(data);
                $("#car_list").append(html);
            },
            error: function (xOptions, textStatus) {
            }
        });
    });
}